﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Threading.Tasks;
using System.Net;
using F3.Maps.OsmParser;

namespace WindowsFormsApplication1
{
    public partial class Parser : Form, IUpdater
    {

        OsmParser osmParser;
        int hrs = 0;
        int mins = 0;
        int sec = 0;
        string title = "Osm Parser";

        public Parser()
        {
            InitializeComponent();
            this.init();          
        }

        /// <summary>
        /// Initialization 
        /// </summary>
        private void init()
        {

            openFileDialog1.Filter = "OpenStreetMaps (*.osm)|*.osm";
            this.tbConnectionString.Text = Application.StartupPath + "\\" + Properties.Settings.Default.Database;
            this.tbTableName.Text = Properties.Settings.Default.TableName;
            this.Text = this.title;
            this.progressBar1.Visible = false;
            this.progressBar1.Style = ProgressBarStyle.Continuous;
            this.label1.Text = "";        
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {

            osmParser = new OsmParser(this, this.tbConnectionString.Text, this.tbTableName.Text);
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.tbPath.Text = openFileDialog1.FileName;
                this.StartParsing();
            }
            
        }

        private void StartParsing()
        {
            this.hrs = 0;
            this.mins = 0;
            this.sec = 0;
            this.timer1.Start();

            Action loading = () =>
            {

                Invoke((MethodInvoker)delegate
                {
                    this.progressBar1.Visible = true;                    
                    this.title = "Loading";
                });

                this.osmParser.LoadOsmMap(openFileDialog1.FileName);

            };

            
            loading.BeginInvoke((asyncResult) =>
            {


                Action parsing = () =>
                {
                    this.title = "Parsing";
                    this.osmParser.Process();

                };
                parsing.BeginInvoke((asyncResult2) =>
                {
                    Invoke((MethodInvoker)delegate
                    {
                        this.progressBar1.Visible = false;                        
                        this.timer1.Stop();

                    });

                }, parsing);

            }, loading);             
        
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            if (sec < 59)
            {
                sec++;
            }
            else
            {
                sec = 0;
                if (mins < 59)
                {
                    mins++;
                }
                else
                {
                    mins = 0;
                    hrs++;
                }
            }


            this.Text = this.title + " " + hrs.ToString().PadLeft(2, '0') + ":" + mins.ToString().PadLeft(2, '0') + ":" + sec.ToString().PadLeft(2, '0');
        }

        public void update(string message, int parsed, int total)
        {
            Invoke((MethodInvoker)delegate
            {
                if(this.progressBar1.Maximum != total)
                {
                    this.progressBar1.Maximum = total;
                }
                this.progressBar1.Value = parsed;
                
                //this.label1.Text = "Parsed "+ parsed + " of " + total;
                this.label1.Text = message;
            });
            
        }

      
    }


   

     
    
    
}
